﻿//===================================================================================
// Microsoft Developer & Platform Evangelism
//=================================================================================== 

using EmitMapper;
using EmitMapper.MappingConfiguration;
using Infrastructure.CrossCutting.SeedWork.Adapter;

namespace Infrastructure.CrossCutting.NetFramework.Adapter
{
    /// <summary>
    ///     Automapper type adapter implementation
    /// </summary>
    public class EmitapperTypeAdapter
        : ITypeAdapter
    {
        #region ITypeAdapter Members

        public TTarget Adapt<TSource, TTarget>(TSource source)
            where TSource : class
            where TTarget : class, new()
        {
            ObjectsMapper<TSource, TTarget> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<TSource, TTarget>(new DefaultMapConfig());
            return mapper.Map(source);
        }

        public TTarget Convert<TSource, TTarget>(TSource source, TTarget target)
            where TTarget : class
            where TSource : class
        {
            ObjectsMapper<TSource, TTarget> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<TSource, TTarget>(new DefaultMapConfig());
            return mapper.Map(source, target);
        }


        public TTarget Convert<TSource, TTarget>(TSource source, TTarget target, object config)
            where TTarget : class
            where TSource : class
        {
            ObjectsMapper<TSource, TTarget> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<TSource, TTarget>((IMappingConfigurator) config);
            return mapper.Map(source, target);
        }


        public TTarget Convert<TSource, TTarget>(TSource source, object config)
            where TTarget : class, new()
            where TSource : class
        {
            ObjectsMapper<TSource, TTarget> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<TSource, TTarget>((IMappingConfigurator) config);
            return mapper.Map(source);
        }

        #endregion
    }
}